package com.suncnpap.reader.controller;

import com.suncnpap.reader.model.Class;
import com.suncnpap.reader.model.Grade;
import com.suncnpap.reader.service.ClassService;
import com.suncnpap.reader.service.GradeService;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.app.util.beanutil.BeanUtil;
import com.app.util.common.Constants;
import com.app.util.common.EasyuiGrid;
import com.app.util.common.HandlerResult;
import com.app.util.common.ObjUtil;
import com.app.util.common.ResponseUtil;
import com.app.util.database.mybatis.Page;
import com.suncnpap.base.common.GlobaUser;
import com.suncnpap.base.common.ParamModel;
import com.suncnpap.base.controller.BaseController;

/** 
 * 说明：班级Controller
 */
@Controller
@RequestMapping(value="/reader/class")
public class ClassController extends BaseController {
	
	//private static final Logger log = Logger.getLogger(ClassController.class);
	
	@Autowired
	ClassService classService;
	
	@Autowired
	GradeService gradeService;
	/**
	 * 列表页面数据处理，包括查询
	 * @param page
	 * @param response
	 */
	@RequestMapping(value="/list")
	public void list(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		HandlerResult result = this.checkToken(request);
		
		if (result.getCode()==0) {

			Page page = this.setPage();
			
			String strGradeId = this.getParameter("strGradeId", "");
			if(!strGradeId.equals("")){
				page.addQueryAnd("strGradeId", strGradeId);
			}
			
			String sort = this.getParameter("sort", "");
			String order = this.getParameter("order", "");
			if(!sort.equals("")){
				page.setSort(sort);
				page.setOrder(order);
			}
			
			page = classService.list(page);
			
			Page gradePage = new Page();
			gradePage.addQueryAnd("strSchoolId", GlobaUser.getStrSchoolId(request.getHeader("token")));
			gradePage.setUsePage(false);
			List<Grade> gradeList = gradeService.list(gradePage).getDataList();
			Map<String, Grade> gradeMap = ObjUtil.listToObjMapByField(gradeList, "strId");
			
			List<Class> classList = page.getDataList();
			for(Class tempClass: classList){
				Grade grade = gradeMap.get(tempClass.getStrGradeId());
				tempClass.setStrGradeName(grade!=null?grade.getStrName():"");
			}
			
			result = this.returnPage(result, page);
		}
		
		ResponseUtil.writeJson(result, response);

	}
	
	/**
	 * 添加处理
	 * @param obj
	 * @param response
	 */
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public void add(@RequestBody Class obj,HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		HandlerResult result = this.checkToken(request);
		if (result.getCode()==0) {
			try{
				super.init(obj);
				classService.add(obj);
			}catch(Exception e){
				result.setCode(1);
				e.printStackTrace();
				throw e;
			}
		}
		ResponseUtil.writeJson(result, response);
	}
	
	@RequestMapping(value="/update",method=RequestMethod.POST)
	public void update(@RequestBody Class obj,HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		HandlerResult result = this.checkToken(request);
		if (result.getCode()==0) {
			try{
				classService.update(obj);
			}catch(Exception e){
				result.setCode(1);
				e.printStackTrace();
				throw e;
			}
		}
		ResponseUtil.writeJson(result, response);
	}
	
	/**
	 * 删除处理
	 * @param strId
	 * @return
	 */
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	public void delete(@RequestBody ParamModel paramModel,HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		HandlerResult result = this.checkToken(request);
		if (result.getCode()==0) {
			try{
				String strId = paramModel.getStrId();
				if(strId != null && !strId.equals("")){
					classService.deleteMore(strId.split(","));
				}
			}catch(Exception e){
				result.setCode(1);
				e.printStackTrace();
				throw e;
			}
		}
		ResponseUtil.writeJson(result, response);
	}
	
	
	@RequestMapping(value="/view")
	public void view(@RequestBody ParamModel paramModel,HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		HandlerResult result = this.checkToken(request);
		if (result.getCode()==0) {
			try{
				String strId = paramModel.getStrId();
				Class obj = classService.findById(strId);
				
				Grade grade = gradeService.findById(obj.getStrGradeId());
				obj.setStrGradeName(grade!=null?grade.getStrName():"");
				
				result.put("obj", obj);
			}catch(Exception e){
				result.setCode(1);
				e.printStackTrace();
				throw e;
			}
		}
		ResponseUtil.writeJson(result, response);
	}
	
	
	
}
